package myjava.thread;

/**
 * @author Yancy
 * @program springioctest
 * @create 2021-03-03
 */
public class MyTread implements Runnable {

    //假设有5张票
    private int ticket = 5;

    @Override
    public void run() {
        for (int i= 0;i<100;i++){
            //对当前对象进行同步
            synchronized (this){
                //还有票
                if (ticket>0){
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("卖票：ticket = " + ticket--);
                }
            }

        }
    }
}
